package com.zky.product.service.impl;

import com.zky.basic.service.IBaiduAiAuditService;
import com.zky.product.domain.Product;
import com.zky.product.domain.ProductDetail;
import com.zky.product.domain.ProductOnlineAuditLog;
import com.zky.product.domain.Product;
import com.zky.product.domain.ProductOnlineAuditLog;
import com.zky.product.mapper.ProductDetailMapper;
import com.zky.product.mapper.ProductMapper;
import com.zky.product.mapper.ProductOnlineAuditLogMapper;
import com.zky.product.service.IProductService;
import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void offsale(List<Long> ids) {

       productMapper.offSale(ids,new Date());

    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);
            if (boo1&&boo2){
               productMapper.onsale(id,new Date());
                //添加日志
                log.setState(1);
                log.setNote("审核成功");
               productOnlineAuditLogMapper.add(log);
            }else {
                log.setState(0);
                log.setNote("审核失败");
               productOnlineAuditLogMapper.add(log);
                return new AjaxResult(false,"审核失败");
            }
        }
        return new AjaxResult();
    }



    @Override
    public void delete(Long id) {
        //删除宠物详情
        productDetailMapper.belByProductId(id);

        super.delete(id);

    }

    @Override
    public void add(Product product) {

        product.setState(0L);
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }

    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }


}
